package org.example.message.service.Impl;

import org.example.message.entity.Message;
import org.example.message.repository.MessageRepository;
import org.example.message.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    private final MessageRepository messageRepository;

    public MessageServiceImpl(MessageRepository messageRepository) {
        this.messageRepository = messageRepository;
    }

    @Override
    @Cacheable(value = "MessageCache", sync = true)
    public List<Message> list(String from, String to){
        List<Message> list=messageRepository.getMessageList(from,to);
        return list;
    }
    @Override
    @CacheEvict(value = "MessageCache", allEntries = true, beforeInvocation = true)
    public void addMessage(Message message){
    messageRepository.addMessage(message);
}

    @Override
    public List<Message> getGroupMessage(String to) {
        return messageRepository.getGroupMessage(to);
    }
}
